<template>
  <div class="bills-management">
    <div class="page-header">
      <h1>账单管理</h1>
      <p class="subtitle">应付账款与应收账款管理</p>
    </div>

    <!-- 账单统计和标签页 -->
    <div class="stats-tabs-section">
      <div class="bills-stats">
        <div class="stat-item">
          <h3>应付账款</h3>
          <p class="amount">¥{{ formatNumber(payableStats.total) }}</p>
          <p class="overdue">逾期: ¥{{ formatNumber(payableStats.overdue) }}</p>
        </div>
        <div class="stat-item">
          <h3>应收账款</h3>
          <p class="amount">¥{{ formatNumber(receivableStats.total) }}</p>
          <p class="overdue">逾期: ¥{{ formatNumber(receivableStats.overdue) }}</p>
        </div>
      </div>

      <div class="tabs">
        <button
          class="tab-btn"
          :class="{ active: activeTab === 'payable' }"
          @click="activeTab = 'payable'"
        >
          应付账款
        </button>
        <button
          class="tab-btn"
          :class="{ active: activeTab === 'receivable' }"
          @click="activeTab = 'receivable'"
        >
          应收账款
        </button>
      </div>
    </div>

    <!-- 应付账款列表 -->
    <div v-if="activeTab === 'payable'" class="bills-content">
      <div class="content-header">
        <h2>应付账款列表</h2>
        <button class="btn btn-primary" @click="showPayableModal = true">
          <span class="btn-icon">+</span>
          新增应付
        </button>
      </div>

      <div class="table-section">
        <div class="table-container">
          <table class="bills-table">
            <thead>
              <tr>
                <th>供应商</th>
                <th>订单号</th>
                <th>应付金额</th>
                <th>已付金额</th>
                <th>未付金额</th>
                <th>到期日</th>
                <th>状态</th>
                <th>操作</th>
              </tr>
            </thead>
            <tbody>
              <tr v-for="item in payableList" :key="item.id">
                <td>{{ item.supplierName }}</td>
                <td>{{ item.poId }}</td>
                <td>¥{{ formatNumber(item.payableAmount) }}</td>
                <td>¥{{ formatNumber(item.paidAmount) }}</td>
                <td>¥{{ formatNumber(item.unpaidAmount) }}</td>
                <td>{{ formatDate(item.dueDate) }}</td>
                <td>
                  <span class="status" :class="getStatusClass(item)">
                    {{ getStatusText(item) }}
                  </span>
                </td>
                <td>
                  <button class="btn btn-small btn-primary" @click="viewPayable(item)">查看</button>
                  <button class="btn btn-small btn-secondary" @click="editPayable(item)">编辑</button>
                </td>
              </tr>
            </tbody>
          </table>
        </div>
      </div>
    </div>

    <!-- 应收账款列表 -->
    <div v-if="activeTab === 'receivable'" class="bills-content">
      <div class="content-header">
        <h2>应收账款列表</h2>
        <button class="btn btn-primary" @click="showReceivableModal = true">
          <span class="btn-icon">+</span>
          新增应收
        </button>
      </div>

      <div class="table-section">
        <div class="table-container">
          <table class="bills-table">
            <thead>
              <tr>
                <th>客户</th>
                <th>订单号</th>
                <th>应收金额</th>
                <th>已收金额</th>
                <th>未收金额</th>
                <th>到期日</th>
                <th>状态</th>
                <th>操作</th>
              </tr>
            </thead>
            <tbody>
              <tr v-for="item in receivableList" :key="item.id">
                <td>{{ item.customerName }}</td>
                <td>{{ item.soId }}</td>
                <td>¥{{ formatNumber(item.invoiceAmount) }}</td>
                <td>¥{{ formatNumber(item.receivedAmount) }}</td>
                <td>¥{{ formatNumber(item.outstandingAmount) }}</td>
                <td>{{ formatDate(item.dueDate) }}</td>
                <td>
                  <span class="status" :class="getStatusClass(item)">
                    {{ getStatusText(item) }}
                  </span>
                </td>
                <td>
                  <button class="btn btn-small btn-primary" @click="viewReceivable(item)">查看</button>
                  <button class="btn btn-small btn-secondary" @click="editReceivable(item)">编辑</button>
                </td>
              </tr>
            </tbody>
          </table>
        </div>
      </div>
    </div>

    <!-- 新增应付账款模态框 -->
    <div v-if="showPayableModal" class="modal-overlay" @click="showPayableModal = false">
      <div class="modal" @click.stop>
        <div class="modal-header">
          <h3>新增应付账款</h3>
          <button class="close-btn" @click="showPayableModal = false">×</button>
        </div>
        <div class="modal-body">
          <form @submit.prevent="createPayable">
            <div class="form-group">
              <label>供应商ID</label>
              <input v-model="newPayable.supplierId" type="number" required>
            </div>
            <div class="form-group">
              <label>采购订单ID</label>
              <input v-model="newPayable.poId" type="number">
            </div>
            <div class="form-group">
              <label>应付金额</label>
              <input v-model="newPayable.payableAmount" type="number" step="0.01" required>
            </div>
            <div class="form-group">
              <label>到期日</label>
              <input v-model="newPayable.dueDate" type="date" required>
            </div>
            <div class="form-actions">
              <button type="button" class="btn btn-secondary" @click="showPayableModal = false">取消</button>
              <button type="submit" class="btn btn-primary">保存</button>
            </div>
          </form>
        </div>
      </div>
    </div>

    <!-- 新增应收账款模态框 -->
    <div v-if="showReceivableModal" class="modal-overlay" @click="showReceivableModal = false">
      <div class="modal" @click.stop>
        <div class="modal-header">
          <h3>新增应收账款</h3>
          <button class="close-btn" @click="showReceivableModal = false">×</button>
        </div>
        <div class="modal-body">
          <form @submit.prevent="createReceivable">
            <div class="form-group">
              <label>客户ID</label>
              <input v-model="newReceivable.customerId" type="number" required>
            </div>
            <div class="form-group">
              <label>销售订单ID</label>
              <input v-model="newReceivable.soId" type="number">
            </div>
            <div class="form-group">
              <label>应收金额</label>
              <input v-model="newReceivable.invoiceAmount" type="number" step="0.01" required>
            </div>
            <div class="form-group">
              <label>到期日</label>
              <input v-model="newReceivable.dueDate" type="date" required>
            </div>
            <div class="form-actions">
              <button type="button" class="btn btn-secondary" @click="showReceivableModal = false">取消</button>
              <button type="submit" class="btn btn-primary">保存</button>
            </div>
          </form>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { apiGet, apiPost } from '../../utils/api.js'

// 响应式数据
const activeTab = ref('payable')
const showPayableModal = ref(false)
const showReceivableModal = ref(false)

// 统计数据
const payableStats = ref({ total: 0, overdue: 0 })
const receivableStats = ref({ total: 0, overdue: 0 })

// 列表数据
const payableList = ref([])
const receivableList = ref([])

// 新增表单数据
const newPayable = ref({
  supplierId: '',
  poId: '',
  payableAmount: '',
  dueDate: ''
})

const newReceivable = ref({
  customerId: '',
  soId: '',
  invoiceAmount: '',
  dueDate: ''
})

// 格式化数字
const formatNumber = (num) => {
  if (!num) return '0.00'
  return new Intl.NumberFormat('zh-CN', {
    minimumFractionDigits: 2,
    maximumFractionDigits: 2
  }).format(num)
}

// 格式化日期
const formatDate = (dateStr) => {
  if (!dateStr) return ''
  return new Date(dateStr).toLocaleDateString('zh-CN')
}

// 获取状态样式类
const getStatusClass = (item) => {
  if (item.overdueStatus === 1) return 'overdue'
  if (item.unpaidAmount > 0 || item.outstandingAmount > 0) return 'pending'
  return 'paid'
}

// 获取状态文本
const getStatusText = (item) => {
  if (item.overdueStatus === 1) return '逾期'
  if (item.unpaidAmount > 0 || item.outstandingAmount > 0) return '待付'
  return '已付'
}

// 获取应付账款数据
const loadPayableData = async () => {
  try {
    const data = await apiGet('/finance/accounts-payable')

    if (data.content) {
      payableList.value = data.content

      // 计算统计
      payableStats.value.total = payableList.value.reduce((sum, item) => sum + Number(item.payableAmount), 0)
      payableStats.value.overdue = payableList.value
        .filter(item => item.overdueStatus === 1)
        .reduce((sum, item) => sum + Number(item.unpaidAmount), 0)
    } else {
      console.error('获取应付账款数据失败')
      // 使用模拟数据
      payableList.value = []
      payableStats.value = { total: 0, overdue: 0 }
    }
  } catch (error) {
    console.error('获取应付账款数据失败:', error)
    payableList.value = []
    payableStats.value = { total: 0, overdue: 0 }
  }
}

// 获取应收账款数据
const loadReceivableData = async () => {
  try {
    const data = await apiGet('/finance/accounts-receivable')

    if (data.content) {
      receivableList.value = data.content

      // 计算统计
      receivableStats.value.total = receivableList.value.reduce((sum, item) => sum + Number(item.invoiceAmount), 0)
      receivableStats.value.overdue = receivableList.value
        .filter(item => item.overdueStatus === 1)
        .reduce((sum, item) => sum + Number(item.outstandingAmount), 0)
    } else {
      console.error('获取应收账款数据失败')
      // 使用模拟数据
      receivableList.value = []
      receivableStats.value = { total: 0, overdue: 0 }
    }
  } catch (error) {
    console.error('获取应收账款数据失败:', error)
    receivableList.value = []
    receivableStats.value = { total: 0, overdue: 0 }
  }
}

// 创建应付账款
const createPayable = async () => {
  try {
    const result = await apiPost('/finance/accounts-payable', {
      ...newPayable.value,
      paidAmount: 0,
      unpaidAmount: newPayable.value.payableAmount,
      overdueStatus: 0
    })

    if (result.code === 0) {
      showPayableModal.value = false
      newPayable.value = { supplierId: '', poId: '', payableAmount: '', dueDate: '' }
      await loadPayableData()
    } else {
      alert(result.message || '创建失败')
    }
  } catch (error) {
    console.error('创建应付账款失败:', error)
    alert('创建失败')
  }
}

// 创建应收账款
const createReceivable = async () => {
  try {
    const result = await apiPost('/finance/accounts-receivable', {
      ...newReceivable.value,
      receivedAmount: 0,
      outstandingAmount: newReceivable.value.invoiceAmount,
      overdueStatus: 0
    })

    if (result.code === 0) {
      showReceivableModal.value = false
      newReceivable.value = { customerId: '', soId: '', invoiceAmount: '', dueDate: '' }
      await loadReceivableData()
    } else {
      alert(result.message || '创建失败')
    }
  } catch (error) {
    console.error('创建应收账款失败:', error)
    alert('创建失败')
  }
}

// 查看应付账款详情
const viewPayable = (item) => {

}

// 编辑应付账款
const editPayable = (item) => {

}

// 查看应收账款详情
const viewReceivable = (item) => {

}

// 编辑应收账款
const editReceivable = (item) => {

}

onMounted(() => {
  loadPayableData()
  loadReceivableData()
})
</script>

<style scoped>
.bills-management {
  padding: 24px;
  max-width: 1200px;
  margin: 0 auto;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  min-height: 100vh;
  position: relative;
}

.bills-management::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-image:
    radial-gradient(circle at 20% 80%, rgba(120, 119, 198, 0.3) 0%, transparent 50%),
    radial-gradient(circle at 80% 20%, rgba(255, 119, 198, 0.3) 0%, transparent 50%),
    radial-gradient(circle at 40% 40%, rgba(120, 219, 255, 0.2) 0%, transparent 50%);
  pointer-events: none;
}

.page-header {
  text-align: center;
  margin-bottom: 32px;
  background: linear-gradient(135deg, rgba(255, 255, 255, 0.9) 0%, rgba(255, 255, 255, 0.7) 100%);
  padding: 24px;
  border-radius: 16px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
  position: relative;
  backdrop-filter: blur(10px);
  border: 1px solid rgba(255, 255, 255, 0.2);
}

.page-header::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 4px;
  background: linear-gradient(90deg, #667eea, #764ba2, #f093fb);
  border-radius: 16px 16px 0 0;
}

.page-header h1 {
  font-size: 2.5rem;
  color: #2c3e50;
  margin-bottom: 8px;
}

.subtitle {
  color: #7f8c8d;
  font-size: 1.1rem;
}

.bills-stats {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 24px;
  margin-bottom: 32px;
}

.stat-item {
  background: linear-gradient(135deg, rgba(255, 255, 255, 0.9) 0%, rgba(255, 255, 255, 0.7) 100%);
  border-radius: 16px;
  padding: 24px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
  text-align: center;
  backdrop-filter: blur(10px);
  border: 1px solid rgba(255, 255, 255, 0.2);
  position: relative;
  overflow: hidden;
}

.stat-item::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 3px;
  background: linear-gradient(90deg, #667eea, #764ba2);
}

.stat-item h3 {
  margin: 0 0 12px 0;
  color: #2c3e50;
  font-size: 1.1rem;
}

.stat-item .amount {
  font-size: 1.8rem;
  font-weight: bold;
  color: #2c3e50;
  margin: 0 0 8px 0;
}

.stat-item .overdue {
  color: #e74c3c;
  font-size: 0.9rem;
  margin: 0;
}

.tabs {
  display: flex;
  margin-bottom: 24px;
  background: linear-gradient(135deg, rgba(255, 255, 255, 0.9) 0%, rgba(255, 255, 255, 0.7) 100%);
  border-radius: 16px;
  padding: 8px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
  backdrop-filter: blur(10px);
  border: 1px solid rgba(255, 255, 255, 0.2);
}

.tab-btn {
  padding: 12px 24px;
  background: none;
  border: none;
  cursor: pointer;
  font-size: 1rem;
  color: #6c757d;
  border-radius: 12px;
  transition: all 0.3s ease;
  position: relative;
  overflow: hidden;
}

.tab-btn::before {
  content: '';
  position: absolute;
  top: 0;
  left: -100%;
  width: 100%;
  height: 100%;
  background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.3), transparent);
  transition: left 0.5s ease;
}

.tab-btn:hover::before {
  left: 100%;
}

.tab-btn.active {
  color: #667eea;
  background: rgba(255, 255, 255, 0.9);
  box-shadow: 0 4px 16px rgba(102, 126, 234, 0.3);
}

.bills-content {
  background: linear-gradient(135deg, rgba(255, 255, 255, 0.9) 0%, rgba(255, 255, 255, 0.7) 100%);
  border-radius: 16px;
  padding: 24px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
  backdrop-filter: blur(10px);
  border: 1px solid rgba(255, 255, 255, 0.2);
  position: relative;
  overflow: hidden;
}

.bills-content::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 3px;
  background: linear-gradient(90deg, #667eea, #764ba2);
}

.content-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 24px;
}

.content-header h2 {
  margin: 0;
  color: #2c3e50;
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 10px 20px;
  border: none;
  border-radius: 12px;
  cursor: pointer;
  font-size: 14px;
  font-weight: 500;
  text-decoration: none;
  transition: all 0.3s ease;
  position: relative;
  overflow: hidden;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.1);
}

.btn::before {
  content: '';
  position: absolute;
  top: 0;
  left: -100%;
  width: 100%;
  height: 100%;
  background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.3), transparent);
  transition: left 0.5s ease;
}

.btn:hover::before {
  left: 100%;
}

.btn-primary {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
}

.btn-primary:hover {
  transform: translateY(-2px);
  box-shadow: 0 8px 25px rgba(102, 126, 234, 0.4);
}

.btn-secondary {
  background: linear-gradient(135deg, #6c757d 0%, #495057 100%);
  color: white;
}

.btn-secondary:hover {
  transform: translateY(-2px);
  box-shadow: 0 8px 25px rgba(108, 117, 125, 0.4);
}

.btn-small {
  padding: 8px 16px;
  font-size: 12px;
}

.btn-icon {
  margin-right: 6px;
  font-weight: bold;
}

.table-section {
  background: linear-gradient(135deg, rgba(255, 255, 255, 0.9) 0%, rgba(255, 255, 255, 0.7) 100%);
  border-radius: 16px;
  padding: 24px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
  backdrop-filter: blur(10px);
  border: 1px solid rgba(255, 255, 255, 0.2);
  position: relative;
  overflow: hidden;
  margin-bottom: 24px;
}

.table-section::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 3px;
  background: linear-gradient(90deg, #667eea, #764ba2);
}

.table-container {
  overflow-x: auto;
}

.bills-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.9rem;
  background: rgba(255, 255, 255, 0.8);
  border-radius: 12px;
  overflow: hidden;
}

.bills-table th,
.bills-table td {
  padding: 16px;
  text-align: left;
  border-bottom: 1px solid rgba(233, 236, 239, 0.5);
}

.bills-table th {
  background: #f8f9fa;
  font-weight: 600;
  color: #495057;
  position: relative;
}

.bills-table th::after {
  content: '';
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  height: 2px;
  background: linear-gradient(90deg, #667eea, #764ba2);
}

.bills-table tbody tr {
  transition: all 0.3s ease;
  border-bottom: 1px solid rgba(233, 236, 239, 0.3);
}

.bills-table tbody tr:hover {
  background: rgba(102, 126, 234, 0.1);
  transform: translateX(4px);
  box-shadow: 0 4px 16px rgba(102, 126, 234, 0.2);
}

.status {
  padding: 8px 12px;
  border-radius: 20px;
  font-size: 12px;
  font-weight: 600;
  display: inline-block;
  text-align: center;
  min-width: 60px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  transition: all 0.3s ease;
}

.status:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.2);
}

.status.overdue {
  background: linear-gradient(135deg, #ff6b6b 0%, #ee5a52 100%);
  color: white;
}

.status.pending {
  background: linear-gradient(135deg, #feca57 0%, #ff9ff3 100%);
  color: white;
}

.status.paid {
  background: linear-gradient(135deg, #48dbfb 0%, #0abde3 100%);
  color: white;
}

.action-btn {
  padding: 4px 8px;
  margin-right: 4px;
  background: #6c757d;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-size: 0.8rem;
  transition: background-color 0.2s ease;
}

.action-btn:hover {
  background: #545b62;
}

.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
}

.modal {
  background: linear-gradient(135deg, rgba(255, 255, 255, 0.95) 0%, rgba(255, 255, 255, 0.9) 100%);
  border-radius: 20px;
  width: 90%;
  max-width: 500px;
  max-height: 90vh;
  overflow: hidden;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
  border: 1px solid rgba(255, 255, 255, 0.2);
  backdrop-filter: blur(20px);
  position: relative;
}

.modal::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 4px;
  background: linear-gradient(90deg, #667eea, #764ba2, #f093fb);
  border-radius: 20px 20px 0 0;
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 24px 24px 20px 24px;
  border-bottom: 1px solid rgba(233, 236, 239, 0.3);
  background: rgba(255, 255, 255, 0.1);
}

.modal-header h3 {
  margin: 0;
  color: #2c3e50;
  font-size: 20px;
  font-weight: 600;
}

.close-btn {
  background: none;
  border: none;
  font-size: 24px;
  cursor: pointer;
  color: #6c757d;
  width: 32px;
  height: 32px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.3s ease;
}

.close-btn:hover {
  background: rgba(108, 117, 125, 0.1);
  color: #495057;
  transform: rotate(90deg);
}

.modal-body {
  padding: 24px;
  max-height: 60vh;
  overflow-y: auto;
}

.modal-body::-webkit-scrollbar {
  width: 6px;
}

.modal-body::-webkit-scrollbar-track {
  background: rgba(0, 0, 0, 0.1);
  border-radius: 3px;
}

.modal-body::-webkit-scrollbar-thumb {
  background: linear-gradient(135deg, #667eea, #764ba2);
  border-radius: 3px;
}

.form-group {
  margin-bottom: 20px;
}

.form-group label {
  display: block;
  margin-bottom: 8px;
  font-weight: 600;
  color: #2c3e50;
  font-size: 14px;
}

.form-group input,
.form-group select {
  width: 100%;
  padding: 12px 16px;
  border: 2px solid rgba(233, 236, 239, 0.5);
  border-radius: 12px;
  font-size: 14px;
  transition: all 0.3s ease;
  background: rgba(255, 255, 255, 0.8);
  box-sizing: border-box;
}

.form-group input:focus,
.form-group select:focus {
  outline: none;
  border-color: #667eea;
  box-shadow: 0 0 0 4px rgba(102, 126, 234, 0.1);
  background: rgba(255, 255, 255, 0.95);
  transform: translateY(-2px);
}

.form-actions {
  display: flex;
  gap: 16px;
  justify-content: flex-end;
  margin-top: 32px;
  padding-top: 20px;
  border-top: 1px solid rgba(233, 236, 239, 0.3);
}

.form-actions button {
  padding: 12px 24px;
  border: none;
  border-radius: 12px;
  cursor: pointer;
  font-size: 14px;
  font-weight: 600;
  transition: all 0.3s ease;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.1);
}

.form-actions button:hover {
  transform: translateY(-2px);
  box-shadow: 0 8px 25px rgba(0, 0, 0, 0.2);
}

@media (max-width: 768px) {
  .bills-management {
    padding: 16px;
  }

  .page-header {
    padding: 20px;
  }

  .page-header h1 {
    font-size: 2rem;
  }

  .bills-stats {
    grid-template-columns: 1fr;
    gap: 16px;
  }

  .tabs {
    flex-direction: column;
    padding: 4px;
  }

  .tab-btn {
    border-radius: 8px;
    margin-bottom: 4px;
  }

  .bills-content {
    padding: 20px;
  }

  .content-header {
    flex-direction: column;
    gap: 16px;
    align-items: stretch;
  }

  .table-section {
    padding: 20px;
  }

  .modal {
    width: 95%;
    margin: 20px;
  }

  .modal-body {
    padding: 20px;
  }

  .form-actions {
    flex-direction: column;
  }

  .form-actions button {
    width: 100%;
  }

  .bills-table {
    font-size: 0.8rem;
  }

  .bills-table th,
  .bills-table td {
    padding: 8px;
  }
}
</style>


